<script setup>
  import { Delete, Edit } from '@element-plus/icons-vue'
  import { ref ,computed} from 'vue'
  import ViewPlan from '@/components/shared/ViewPlan.vue'

  import { getStudentListService, getClassListService } from '@/api/user'

  const drawer = ref()
  const loading = ref(false)
  const studentList = ref([])
  const selectedList = ref([])
  const classList = ref([])
  const selectClass = ref(null)
  const pageSize=ref(15)
  const currentPage=ref(1)
  const getClassList = async () => {
    const res = await getClassListService()
    classList.value = res.data
  }
  getClassList()
  const getStudentList = async () => {
    loading.value = true
    const res = await getStudentListService()
    studentList.value = res.data.student;
    selectedList.value = studentList.value;
    loading.value = false
  }
  getStudentList()
  const selectStudentList = () => {
    loading.value = true
    selectedList.value = studentList.value.filter((item) => item.klass.code == selectClass.value.code)
    loading.value = false
  }
  const onEdit = (row) => {
    drawer.value.open(row)

  }


  const onSearch = () => {
    selectStudentList()
  }
  const onReset = () => {

    getStudentList()
  }
  const onSuccess = () => {
    getStudentList()
  }
  const paginatedData = computed(() => {
    const start = (currentPage.value - 1) * pageSize.value;
    const end = start + pageSize.value;
    return selectedList.value.slice(start, end);
  });
  const handlePageChange = (pageNum) => {
    currentPage.value = pageNum
    getStudentList()
  }
  const handleSizeChange = (size) => {
    pageSize.value = size;
    getStudentList()
  }
</script>

<template>
  <page-container title="计划总览">
    <el-form inline>
      <el-form-item label="班级：">
        <el-select v-model="selectClass" style="width: 220px" placeholder="请选择班级" value-key="_id">
          <el-option v-for="item in classList" :key="item._id" :label='item.code' :value="item">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSearch">搜索</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="paginatedData" style="width: 100%" v-loading="loading">
      <el-table-column label="班级">
        <template #default="scope">
          <span>{{scope.row.klass.code}}</span>
        </template>
      </el-table-column>
      <el-table-column label="学号" prop="code"></el-table-column>
      <el-table-column label="姓名" prop="name"></el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">

          <el-button size="small" @click="onEdit(row)">编辑</el-button>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="没有数据" />
      </template>
    </el-table>
    <el-pagination background layout="total,prev, pager, next" :total="selectedList.length"
      @current-change="handlePageChange" @size-change="handleSizeChange" :current-page="currentPage"
      :page-size="pageSize" style="margin-top: 20px; 
    justify-content: flex-end" />
    <view-plan ref="drawer"></view-plan>
  </page-container>

</template>

<style lang="scss" scoped></style>